﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Hangfire;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System.Text;
using WYT.Ord.Application.Contracts.Dtos.Order;
using WYT.Ord.Application.Contracts.Services;
using System.Text.Json;

namespace WYT.Ord.WebApi.Controllers
{
    /// <summary>
    /// 订单
    /// </summary>
    [Route("ord/[Controller]")]
    [ApiController]
    public class OrderController : AdncControllerBase
    {
        private readonly IOrderAppService _ordAppService;
        private readonly IUserContext _userContext;

        public OrderController(
            IOrderAppService ordAppService,
            IUserContext userContext)
        {
            _ordAppService = ordAppService;
            _userContext = userContext;
        }

        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Submit")]
        public async Task<WYTActionResult<long>> SubmitOrder(OrderCreationDto input)
        {
            return Success(await _ordAppService.OrderCreate(_userContext.Id, input));
        }


        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("Detail")]
        public async Task<WYTActionResult<OrderDetailDto>> GetOrderDetail([FromQuery] SimpleInputDto<long> input)
        {
            return Success(await _ordAppService.GetOrderDetail(input.Value));
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input">0：全部 2：待付款 4：待收货 8：待评论</param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<WYTActionResult<PageModelDto<OrderListDto>>> OrderListSearch([FromQuery] SearchPagedDto<int> input)
        {
            return Success(await _ordAppService.OrderListSearch(_userContext.Id, input));
        }

        /// <summary>
        /// ERP回调
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("erpCallBack")]
        [AllowAnonymous()]
        public async Task<ActionResult> ErpCallBack([FromBody] ERPCallbackDto input)
        {
            return Result(await _ordAppService.ErpCallBack(input));
        }

        /// <summary>
        /// 物流追踪
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("LogisticsTracking")]
        public async Task<WYTActionResult<List<LogisticsTrackingDto>>> LogisticsTracking([FromQuery] SimpleDto<long> input)
        {
            return Success(await _ordAppService.LogisticsTracking(input.Value));
        }

        /// <summary>
        /// 收货确认
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Receiving")]
        public async Task<WYTActionResult> ReceiptConfirmation(SimpleDto<long> input)
        {
            return Success(await _ordAppService.ReceiptConfirmation(input.Value));
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Review")]
        public async Task<WYTActionResult> AddReview(GoodsReviewAddDto input)
        {
            return Success(await _ordAppService.AddReview(_userContext.Id, input));
        }


        #region 内部接口

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("_Detail")]
        [AllowAnonymous]
        public async Task<ActionResult<OrderDetailDto>> RPCGetOrderDetail([FromQuery] SimpleInputDto<long> input)
        {
            return Result(await _ordAppService.GetOrderDetail(input.Value));
        }

        #endregion
    }
}
